#-*- coding: big5 -*-

intMaxPageHeight=3

class Page:
    def __init__(self):
        self.intPageHeight=0
        self.strPageContent=''
    def canAddLine(self):
        if self.intPageHeight+1>intMaxPageHeight: return False
        return True
    def addLine(self,strLine):
        self.strPageContent+=strLine+'\n'
        self.intPageHeight+=1
        
class PageBuilder:
    def __init__(self):
        self.clear()
    def clear(self):
        self.lstPage=[]
        self.intPageCursor=0
    def getNextPageString(self):
        self.intPageCursor+=1
        return self.lstPage[self.intPageCursor].strPageContent
    def getPreviousPageString(self):
        self.intPageCursor-=1
        return self.lstPage[self.intPageCursor].strPageContent
    def assignBookAndDividePage(self,book):
        strBook=self.getStrBook(book)
        lstStrLineOfBook=strBook.split('\n')
        page=Page()
        for strLineOfBook in lstStrLineOfBook:
            if page.canAddLine():
                page.addLine(strLineOfBook)
            else:
                self.lstPage.append(page)
                page=Page()
                page.addLine(strLineOfBook)
    def getPageString(self,intPageNum):
        return self.lstPage[intPageNum].strPageContent
    def traverseAssignStrBook(self,lstBookComponent):
       if not lstBookComponent: return
       for bookComponent in lstBookComponent:
           if bookComponent.isLeaf():
               self.strBook+=' '*self.intIndent+bookComponent.strText+'\n'
           else:
               if bookComponent.strText:
                   self.strBook+=' '*self.intIndent+bookComponent.strText+'\n'
               self.intIndent+=4
               self.traverseAssignStrBook(bookComponent.lstBookComponent)
               self.intIndent-=4
    def getStrBook(self,book):
        self.strBook=''
        self.intIndent=0
        self.strBook+='Book Title:%s'%book.strText+'\n'
        self.strBook+='Author:%s'%book.strAuthor+'\n'
        self.traverseAssignStrBook(book.lstBookComponent)
        return self.strBook
    